using System.Collections;
using System.Collections.Generic;
using MyProject.Persona;
using UnityEngine;

namespace MyProject.Persona
{
    public class InitialState : BaseState
    {
        // Start is called before the first frame update
        public InitialState(Personas persona, PlayerController player) : base(persona, player)
        {
        }
    
        public override void OnStart()
        {
            player.MaxFall = Constants.maxFall;
            player.jumpCount = 1;
        }
    
        public override void OnEnd()
        {
            
        }
    
        public override Personas LogicUpdate()
        {
            switch (player.curPersona)
            {
                case Personas.Blue:
                    if (!player.startConstantSpeed)
                    {
                        if (player.rb.velocity.y < 0 && !player.physicsCheck.isGround && GameInput.jump.Checked())
                        {
                            player.rb.velocity += Vector2.up *
                                                  (Physics2D.gravity.y * (player.paopaoFallMultiplier - 1) * Time.deltaTime);
                        }
                        else if(player.rb.velocity.y < 0 && !player.physicsCheck.isGround && !GameInput.jump.Checked())
                        {
                            player.rb.velocity += Vector2.up *
                                                  (Physics2D.gravity.y * (player.fallMultiplier - 1) * Time.deltaTime);
                        }
                    }
                    else
                    {
                        if (player.rb.velocity.y < 0 && !player.physicsCheck.isGround)
                        {
                            player.rb.velocity = new Vector2(player.rb.velocity.x, player.paopaoFallSpeed);
                        }
                    }
                    
                    if (GameInput.jump.Press())
                    {
                        if(player._jumpCheck.AllowJump())
                            player.rb.AddForce(player.transform.up * player.paopaoJumpForce, ForceMode2D.Impulse);
                    }
                    break;
                default:
                    //计算竖直速度
                    {
                        //修改最大速度，防止高空坠落穿过去
                        {
                            // float maxFallSpeed = Constants.maxFall;
                            // float fastMaxFallSpeed = Constants.FastMaxFall;
                            //
                            // if (player.rb.velocity.y <= maxFallSpeed)
                            // {
                            //     var vector2 = player.rb.velocity;
                            //     vector2.y = Mathf.MoveTowards(player.rb.velocity.y, maxFallSpeed,
                            //         Constants.FastMaxAccel * Time.deltaTime);
                            //     player.rb.velocity = vector2;
                            // }
                            // else
                            // {
                            //     Vector2 vector2 = player.rb.velocity;
                            //     vector2.y = Mathf.MoveTowards(player.rb.velocity.y, fastMaxFallSpeed,
                            //         Constants.FastMaxAccel * Time.deltaTime);
                            //     player.rb.velocity = vector2;
                            // }
                            
                            if (!player.startConstantSpeed)
                            {
                                if (player.rb.velocity.y < 0 && !player.physicsCheck.isGround)
                                {
                                    player.rb.velocity += Vector2.up *
                                                          (Physics2D.gravity.y * (player.fallMultiplier - 1) * Time.deltaTime);
                                    player.rb.velocity = new Vector2(player.rb.velocity.x, Mathf.Min(Constants.maxFall, player.rb.velocity.y));
                                }
                                else if (player.rb.velocity.y > 0)
                                {
                                    player.rb.velocity += Vector2.up *
                                                          (Physics2D.gravity.y * (player.lowJumpMultiplier - 1) *
                                                           Time.deltaTime);
                                }
                            }
                            else
                            {
                                if (player.rb.velocity.y < 0 && !player.physicsCheck.isGround)
                                {
                                    player.rb.velocity = new Vector2(player.rb.velocity.x, player.fallSpeed);
                                }
                            }
                        }
                    }

                    {
                        if (player.varJumpTimer > 0)
                        {
                            if (GameInput.jump.Checked())
                            {
                                player.rb.velocity = new Vector2(player.rb.velocity.x,
                                    Mathf.Max(player.rb.velocity.y, player.varJumpSpeed));
                            }
                            else
                            {
                                player.varJumpTimer = 0;
                            }
                            
                        }
                        
                        if (GameInput.jump.Press())
                        {
                            if (player._jumpCheck.AllowJump())
                            {
                                Debug.Log("1");
                                player.Jump();
                            }
                            else
                            {
                                player.AdvanceJump();
                            }
                        }
                        
                        if (player.inputAdvance)
                        {
                            if (player.canInputAdvance && player.physicsCheck.isGround)
                            {
                                player.Jump();
                                player. canInputAdvance = false;
                            }
                        }
                        
                    }
                    break;
            }

            // {
            //     if (GameInput.jump.Checked())
            //     {
            //         jumpCount += Time.deltaTime;
            //         player.Jump();
            //         pressDown = true;
            //         //Debug.Log(jumpCount);
            //     }
            //
            //     if (GameInput.jump.Up() && pressDown)
            //     {
            //         if (jumpCount <= player.varJumpTimer && player.jumpCount > 0)
            //         {
            //             player.rb.AddForce(player.transform.up * player.smallJumpForce, ForceMode2D.Impulse);
            //             player.jumpCount -= 1;
            //             pressDown = false;
            //             Debug.Log(player.jumpCount);
            //         }
            //         jumpCount = 0;
            //     }
            //     
            //     if (player.physicsCheck.isGround)
            //     {
            //         Debug.Log(player.jumpCount);
            //         player.jumpCount = 1;
            //     }
            // }
    
            return persona;
        }
    }
}

